package com.wythe.mall.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.activity.GoodsActivity;
import com.wythe.mall.adapter.GridViewAdapter;
import com.wythe.mall.adapter.Type;
import com.wythe.mall.intf.OnRecyclerViewItemClickListener;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ClassifyBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author anumbrella
 * @date 2015-8-1
 * <p>
 * 用于右侧栏显示详细信息
 * <p>
 * (滑动的ViewPager,每一个滑动的页面都是一个fragment)
 */
public class TypeFragment extends Fragment implements View.OnClickListener {

    private ArrayList<ClassifyBean.GoodsVoListBean> listType;
    // widget网格view
    private RecyclerView gridView;
    // 网格适配器
    private GridViewAdapter adapter;

    private LinearLayout llZhuangchang;

    // 右侧栏目中的子选项
    private Type type;

    // 子选项名称
    private String typeName = "东江湖蜜桔";

    //顶部图片
    private ImageView topImage;

    // 图片
    private int icon;

    private int mid;

    private View rootView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mid = getArguments().getInt("id");
    }

//    private void initData() {
//        JSONObject param = new JSONObject();
//		try {
//			param.put("classify", id);
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
//		ApiOkHttp.getClassifyShop(getActivity(), param, new OnRequestDataListener() {
//            @Override
//            public void requestSuccess(int code, JSONObject data) {
//                if (listType == null) {
//					listType = new ArrayList<>();
//                }
//				listType.clear();
//                JSONArray array = null;
//                try {
//                    array = data.getJSONArray("data");
//                    for (int i = 0; i < array.length(); i++) {
//                        ClassifyBean.GoodsVoListBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ClassifyBean.GoodsVoListBean.class);
//						listType.add(bean);
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//            }
//
//            @Override
//            public void requestFailure(int code, String msg) {
//
//            }
//        });
//    }

    private void getShopData(int id) {
        JSONObject param = new JSONObject();
        try {
            param.put("classify", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.getShopLabels(getActivity(), param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                if (listType == null) {
                    listType = new ArrayList<>();
                }
                listType.clear();
                JSONArray array = null;
                try {
                    array = data.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        ClassifyBean.GoodsVoListBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ClassifyBean.GoodsVoListBean.class);
                        listType.add(bean);
                    }
                    if (adapter == null) {
                        adapter = new GridViewAdapter(getActivity(), listType);
                    }
                    adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
                        @Override
                        public void onRecyclerViewItemClick(View view, int position) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("id", listType.get(position).getId());
                            GotoActivity.gotoActiviy(getActivity(), GoodsActivity.class, bundle);
                        }
                    });
                    final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    gridView.setLayoutManager(manager);
                    gridView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getShopData(mid);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.shopslist_layout, container,false);
        }
        ViewGroup mViewGroup = (ViewGroup) rootView.getParent();
        if (mViewGroup != null) {
            mViewGroup.removeView(rootView);
        }
        gridView = (RecyclerView) rootView.findViewById(R.id.GridViewList);

        // 为listType装载数据
        setTypeList();
        return rootView;
    }

    /**
     * 给listType设定数据
     */
    private void setTypeList() {
        // TODO Auto-generated method stub
//		listType = new ArrayList<>();
        // 这里可以根据数据设定要填充的资源
//		for (int i = 0; i < 23; i++) {
//			type = new Type(i, icon, typeName + i);
//			listType.add(type);
//		}
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//			case R.id.shop_zhuanchang:{
//				GotoActivity.gotoActiviy(getActivity(), ProductActivity.class);
//				break;
//			}
        }
    }
}
